/*
 * Copyright 1999-2019 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.csp.sentinel.demo.spring.web;

import java.util.concurrent.ThreadLocalRandom;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Eric Zhao
 */
@RestController
public class BazController {

	@RequestMapping("/execption-rule")
	@SentinelResource(value = "ERROR_RATIO", defaultFallback = "defaultFallback", 
		blockHandler = "handleStringException",
	 exceptionsToIgnore = {
	 		IllegalStateException.class }
	)
	public String execptionRule() {

		if (ThreadLocalRandom.current().nextInt(0, 100) > 55) {

			throw new RuntimeException("oops");

		}
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "no execption and block";
		// model.addAttribute("greeting", greetingClient.greeting());
		// return "greeting-view";
	}
	
	@RequestMapping("/Interceptor") 
	@ResponseBody  
	public String InterceptorTest()  {
		int o=ThreadLocalRandom.current().nextInt(0, 100);
		if ( o > 55) {
			System.out.println("RuntimeException....");
			throw new RuntimeException("oops");
		}
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "no execption and block";
		// model.addAttribute("greeting", greetingClient.greeting());
		// return "greeting-view";
	}
//	
//	@ExceptionHandler(RuntimeException.class)
//    public String handleInvalidTokenException(RuntimeException e) {
//        
//		
//        return e.getLocalizedMessage();
//    }
	
	
	@RequestMapping("/Interceptor/test") 
	public String InterceptorTest2() throws   ArithmeticException{

		if (ThreadLocalRandom.current().nextInt(0, 100) > 55) {

			throw new IllegalStateException("Interceptor/test");

		}
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "no execption and block";
		// model.addAttribute("greeting", greetingClient.greeting());
		// return "greeting-view";
	}
	
	@RequestMapping("/other") 
	public String other() {

		if (ThreadLocalRandom.current().nextInt(0, 100) > 55) {

			throw new RuntimeException("oops");

		}
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "no execption and block";
		// model.addAttribute("greeting", greetingClient.greeting());
		// return "greeting-view";
	}
	
	public String defaultFallback(Throwable e) {
		if (e  instanceof BlockException) {
				BlockException b = (BlockException) e;
				return e.getClass().getCanonicalName() + " caused by:" + b.getRule().toString();
			 
		} else
			return e.getClass().getCanonicalName() + " be throwabled by:" + e.getLocalizedMessage();

	}

   
//	public String defaultFallback() {
//		return "Hello from defaultFallback";
//	}

}
